package cn.it.hbtt.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.it.hbtt.daoimg.Orderimg;
import cn.it.hbtt.entity.User;

/**
 * Servlet implementation class OrderServlet
 * 
 * 实现生成订单号和调用添加方法向数据库中添加数据
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int math = (int) (Math.random() * 9000 + 1000);
		Date date = new Date();
		SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = time1.format(date);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");

		String aa = dateFormat.format(date) + math;// 订单号
		User user = (User) request.getSession().getAttribute("q");
		int id = user.getId();
		String loginName = user.getLoginName();
		
		HttpSession session = request.getSession();
		float cost = (float) session.getAttribute("q3");// 消费金额
		//float cost=(Float) null;
		Orderimg order = new Orderimg();
		order.m(id, loginName, aa, time,cost, aa);
		HttpSession s = request.getSession();
		s.setAttribute("cost", aa);

		order.drop_order(id);

		String str = "<script  type='text/javascript'>";
		str += "alert('确认成功！');";
		str += "location.href='http://localhost:8080/easybuy/BuyCar_Three.jsp'";
		str += "</script>";
		out.write(str);
		out.flush();

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
